package com.curluin.approximateShapes;

import java.util.Vector;

import com.curluin.math.MyVector;
import com.curluin.scene.Node;

public class Ellipse extends ApproximateShape {

	
	protected static String LOG_TAG="Ellipse";
	/**
	 * Three dimensiononal vector defining the center of the circle 
	 */
	private MyVector center;
	
	/**
	 * radius of the circle
	 */
	private float radiusX;
	private float radiusZ;
	
	
	
	
	public Ellipse(Node node) {
		radiusX=node.outerSize.get(0)/2;
		radiusZ=node.outerSize.get(2)/2;
		this.center = node.getCenter();
//		Log.w(LOG_TAG,"center:"+center+" radius:"+radius+" size:"+node.outerSize);
	}
	/**
	 * Computes all z-coordinates of the shape lying on the specified x-coordinate,
	 * the line between two returned vectors is occupied by the shape (even=start points,uneven=end points).
	 * @param x
	 * @return vectors, each two define a line
	 */
	private Vector<MyVector> computeSingleXScan(float x) {
		Vector<MyVector> res= new Vector<MyVector>();
		
		float arg = radiusX*radiusX-x*x;
		arg= (arg<0&&arg>-0.001)? 0 : arg;
		float z = (radiusZ/radiusX)*(float)Math.sqrt(arg);
//		Log.e(LOG_TAG, radiusX+"*"+radiusX+"-"+(x*x)+" = "+(radiusX*radiusX-x*x));

		MyVector result1=MyVector.makeVector3(x,center.get(1),z).add(center);
		MyVector result2=MyVector.makeVector3(x,center.get(1),-z).add(center);
//		Log.e(LOG_TAG, "c="+center+" r="+radiusX+" Schnittpunkt mit x="+x+" :"+result1+" und:"+result2);
		res.add(result1);
		res.add(result2);
		return res;
	}

	
	@Override
	protected Vector<MyVector> initializeScanData(float sampleRate) {

		Vector<MyVector> res = new Vector<MyVector>();
		float xmin=-radiusX-MyVector.epsilon;
		float xmax=radiusX+MyVector.epsilon;
		while(xmin<xmax){
			res.addAll(computeSingleXScan(xmin));
			xmin+=sampleRate;
		}
		res.addAll(computeSingleXScan(xmax));
		scanData=res;
		return res;
	}

	
	
	/* (non-Javadoc)
	 * @see com.curluin.approximateShapes.ApproximateShape#translateScanData(com.curluin.math.MyVector)
	 */
	@Override
	public Vector<MyVector> translateScanData(MyVector translation) {
//		Log.w(LOG_TAG,"Center: "+center);
		center=center.add(translation);
		return super.translateScanData(translation);
	}
	
	

}
